PROJPATH			:= $(abspath ../../../)
# also for Kconfig
export BOARDPATH	:= $(abspath ./)

export KCONFIG_TOOL	:= kconfig-mconf
export PYTHON_TOOL	:= python3
OUTPUT				:= $(BOARDPATH)/build
# compiler arguments
OBJDUMP				:= aarch64-none-linux-gnu-objdump
OBJCOPY				:= aarch64-none-linux-gnu-objcopy
export TARGET		:= matrix
export CC			:= aarch64-none-linux-gnu-gcc
export AS			:= aarch64-none-linux-gnu-gcc
export LD			:= aarch64-none-linux-gnu-ld
export CFLAGS		:= -c -nostdlib -Wl,-gc-sections -march=armv8.1-a+nofp+nosimd -O0 -g
export SFLAGS		:= -c -nostdlib -Wl,-gc-sections -march=armv8.1-a+nofp+nosimd -O0 -g
export LFLAGS		:= -Map $(OUTPUT)/$(TARGET).map -T$(BOARDPATH)/meta/board/link.lds
# source file path
export SRCDIRS		:=  $(BOARDPATH)/src	\
						$(BOARDPATH)/drv	\
						$(BOARDPATH)/lib
# header file path
export INCDIRS		:=  $(BOARDPATH)		\
						$(BOARDPATH)/src	\
						$(BOARDPATH)/drv	\
						$(BOARDPATH)/lib
# meta
export META_SRCDIRS	:= $(BOARDPATH)
export META_INCDIRS	:= $(BOARDPATH)
export META_CC		:= aarch64-none-linux-gnu-gcc -E -P

# set default target
.DEFAULT_GOAL 		:= ALL

# -------- kernel rules
ALL:
	@rm -rf $(OUTPUT)/$(TARGET).elf
	@$(MAKE) -C $(PROJPATH)
	@$(OBJCOPY) -O binary $(OUTPUT)/$(TARGET).elf $(OUTPUT)/$(TARGET).bin
	@$(OBJDUMP) -D $(OUTPUT)/$(TARGET).elf > $(OUTPUT)/$(TARGET).dump
	@nm -g $(OUTPUT)/$(TARGET).elf | awk '$$2=="T" && $$3 != "main" && $$3 != "__start" {print $$3 " = 0x" $$1 ";"}' > $(OUTPUT)/$(TARGET).sym

# -------- kernel app rules (shared object file)
export KAPP_CFLAGS	:= -O0 -g
export KAPP_SFLAGS	:= -O0 -g
kapp:
	@$(MAKE) -C $(PROJPATH) kapp

kapp-clean:
	@$(MAKE) -C $(PROJPATH) kapp-clean

# -------- pseudo target
.PHONY: menuconfig clean distclean help run debug

menuconfig:
	@$(MAKE) -C $(PROJPATH) menuconfig
clean:
	@$(MAKE) -C $(PROJPATH) clean
distclean:
	@$(MAKE) -C $(PROJPATH) distclean
help:
	@$(MAKE) -C $(PROJPATH) help

run:
	@. ./qemu-virt-max-start.sh
debug:
	@. ./qemu-virt-max-debug.sh
